package com.echo.fm.log.system;

import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.echo.fm.log.system.model.SystemLog;
import com.echo.fm.platform.database.IHibernatePersistence;
import com.echo.util.ErrorException;
import com.echo.util.ResultUtils;

@Repository("systemLogPersistence")
public class DaoSystemLogPersistenceImpl implements ISystemLogPersistence {
	@Autowired
	@Qualifier("hibernatePersistence")

	IHibernatePersistence hibernatePersistence;

	public void createSystemLog(SystemLog systemLog) throws ErrorException {
		Session session = null;
		Transaction tx = null;
		try {
			session = hibernatePersistence.openSession();

			BigInteger seqVal = (BigInteger) session.createSQLQuery("select nextVal('system_log_seq')").uniqueResult();
			Calendar calendar = Calendar.getInstance(Locale.CHINA);
			String createDate = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
			systemLog.setId(createDate.replace("-", "") + String.format("%09d", seqVal));
			systemLog.setCreateTime(calendar.getTimeInMillis());
			tx = session.beginTransaction();
			session.save(systemLog);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			if (tx != null) {
				tx.rollback();
			}
			throw new ErrorException(ResultUtils.WEB_DB_SERVER_ERROR_CODE);
		} finally {
			hibernatePersistence.closeSession(session);
		}

	}

	public void updateSystemLog(SystemLog systemLog) throws ErrorException {
		hibernatePersistence.update(systemLog);
	}

	public void deleteSystemLog(SystemLog systemLog) throws ErrorException {
		hibernatePersistence.delete(systemLog);
	}

	public SystemLog searchSystemLogById(String id) throws ErrorException {
		return (SystemLog) hibernatePersistence.searchById(SystemLog.class, Restrictions.eq("id", id));
	}

	public List<SystemLog> searchSystemLogList(List<Criterion> criterions) throws ErrorException {
		@SuppressWarnings("unchecked")
		List<SystemLog> resultList = (List<SystemLog>) hibernatePersistence.searchList(SystemLog.class, criterions);
		return resultList;
	}

	public List<SystemLog> pagesSystemLogList(List<Criterion> criterions, int firstResult) throws ErrorException {
		@SuppressWarnings("unchecked")
		List<SystemLog> resultList = (List<SystemLog>) hibernatePersistence.pagesList(SystemLog.class, criterions,
				Order.desc("createTime"), firstResult);
		return resultList;
	}

	public int pagesSystemLogCountList(List<Criterion> criterions) throws ErrorException {
		return hibernatePersistence.pagesCount(SystemLog.class, criterions);
	}

}
